home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 November: Tool Chest / Dev.CD Nov 00 TC Disk 1.toast / Sample Code / Networking / OTLookupNameTest / OTLookupNameTest.c next >
Encoding:
C/C++ Source or Header  |  2000-09-28  |  6.9 KB  |  246 lines  |  [TEXT/CWIE]

  1. /*
  2.     File:        OTLookupNameTest.c
  3.  
  4.     Contains:    A simple illustration of how to use OTLookupName.
  5.  
  6.     Written by: Quinn "The Eskimo!"    
  7.  
  8.     Copyright:    Copyright © 1997-1999 by Apple Computer, Inc., All Rights Reserved.
  9.  
  10.                 You may incorporate this Apple sample source code into your program(s) without
  11.                 restriction. This Apple sample source code has been provided "AS IS" and the
  12.                 responsibility for its operation is yours. You are not permitted to redistribute
  13.                 this Apple sample source code as "Apple sample source code" after having made
  14.                 changes. If you're going to re-distribute the source, we require that you make
  15.                 it clear in the source that the code was descended from Apple sample source
  16.                 code, but that you've made changes.
  17.  
  18.     Change History (most recent first):
  19.                 7/22/1999    Karl Groethe    Updated for Metrowerks Codewarror Pro 2.1
  20.                 
  21.  
  22. */
  23.  
  24. /////////////////////////////////////////////////////////////////////
  25. // The OT debugging macros in <OTDebug.h> require this variable to
  26. // be set.
  27. #include <Events.h>
  28. #ifndef qDebug
  29. #define qDebug    1
  30. #endif
  31.  
  32. /////////////////////////////////////////////////////////////////////
  33. // Pick up all the standard OT stuff.
  34.  
  35. #include <OpenTransport.h>
  36.  
  37. /////////////////////////////////////////////////////////////////////
  38. // Pick up all the OT AppleTalk stuff.
  39.  
  40. #include <OpenTptAppleTalk.h>
  41.  
  42. /////////////////////////////////////////////////////////////////////
  43. // Pick up the OTDebugBreak and OTAssert macros.
  44.  
  45. #include <OTDebug.h>
  46.  
  47. /////////////////////////////////////////////////////////////////////
  48. // Standard C prototypes.
  49.  
  50. #include <stdio.h>
  51.  
  52. /////////////////////////////////////////////////////////////////////
  53. // OTDebugStr is not defined in any OT header files, but it is
  54. // exported by the libraries, so we define the prototype here.
  55.  
  56. extern pascal void OTDebugStr(const char* str);
  57.  
  58. /////////////////////////////////////////////////////////////////////
  59.  
  60. static UInt32 gLastPrinted = 0;
  61.  
  62. static pascal void YieldingNotifier(EndpointRef ep, OTEventCode code, 
  63.                                        OTResult result, void* cookie)
  64.     // This notifier calls printf on a period basis in response
  65.     // to a kOTSyncIdleEvent.  By the wonders of SIOUX, printf calls
  66.     // WaitNextEvent for us, and hence our synchronous calls to 
  67.     // OT will yield to other processes.
  68. {
  69.     #pragma unused(ep)
  70.     #pragma unused(result)
  71.     #pragma unused(cookie)
  72.     
  73.     switch (code) {
  74.         case kOTSyncIdleEvent:
  75.             if (TickCount() > gLastPrinted + 10) {
  76.                 printf(".");
  77.                 fflush(stdout);
  78.                 gLastPrinted = TickCount();
  79.             }
  80.             break;
  81.         default:
  82.             // do nothing
  83.             break;
  84.     }
  85. }
  86.  
  87. /////////////////////////////////////////////////////////////////////
  88.  
  89. static void PrintAddress( DDPAddress *addr )
  90.     // Prints a DDPAddress in a nicely formatted manner.
  91. {
  92.     OTAssert( "PrintAddress: Expected a DDPNBPADdress", addr->fAddressType == AF_ATALK_DDP );
  93.     printf("Net = $%04x, Node = $%02x, Socket = $%02x ", 
  94.                 addr->fNetwork, 
  95.                 addr->fNodeID, 
  96.                 addr->fSocket);
  97. }
  98.  
  99. static void PrintName(const char *name, UInt32 length)
  100.     // Prints on NBP name in a nicely formatted manner.
  101. {
  102.     char nameForPrinting[256];
  103.     
  104.     OTMemzero(nameForPrinting, 256);
  105.     OTMemcpy(nameForPrinting, name, length);
  106.  
  107.     printf("“%s”", nameForPrinting);
  108. }
  109.  
  110. enum {
  111.     kResponseBufferSize = 8192
  112. };
  113.  
  114. static OSStatus LookupAndPrint(char *requestAddress)
  115.     // requestAddress must be an NBP address of the form
  116.     // <name>:<type>@<zone>.  This routine opens an NBP mapper
  117.     // provider, switches it into synchronous/blocking mode
  118.     // and uses sync idle events to yield time to other processes.
  119.     // It then issues an NBP lookup request using OTLookupName.
  120.     // When the request completes, it prints out the resulting
  121.     // addresses and names.
  122. {
  123.     OSStatus err;
  124.     OSStatus junk;
  125.     MapperRef nbpMapper;
  126.     TLookupRequest lookupRequest;
  127.     TLookupReply lookupReply;
  128.     UInt8 *responseBuffer;
  129.     TLookupBuffer *currentLookupReplyBuffer;
  130.     UInt32 nameIndex;
  131.     
  132.     err = noErr;
  133.     nbpMapper = kOTInvalidMapperRef;
  134.     
  135.     // Create the responseBuffer.
  136.     
  137.     responseBuffer = OTAllocMem(kResponseBufferSize);
  138.     if (responseBuffer == nil) {
  139.         err = kENOMEMErr;
  140.     }
  141.     
  142.     // Create an NBP mapper and set it to up for threaded processing.
  143.     
  144.     if (err == noErr) {
  145.         nbpMapper = OTOpenMapper(OTCreateConfiguration(kNBPName), 0, &err);
  146.     }
  147.     if (err == noErr) {
  148.         junk = OTSetSynchronous(nbpMapper);
  149.         OTAssert("LookupAndPrint: Could not set synchronous mode on mapper", junk == noErr);
  150.         junk = OTSetBlocking(nbpMapper);
  151.         OTAssert("LookupAndPrint: Could not set blocking mode on mapper", junk == noErr);
  152.         junk = OTUseSyncIdleEvents(nbpMapper, true);
  153.         OTAssert("LookupAndPrint: Could not enable sync idle events on mapper", junk == noErr);
  154.         junk = OTInstallNotifier(nbpMapper, YieldingNotifier, nil);
  155.         OTAssert("LookupAndPrint: Could not install notifier into mapper", junk == noErr);
  156.     }
  157.     
  158.     // Issue the OTLookupName synchronously.
  159.     
  160.     if (err == noErr) {
  161.         
  162.         // Set up the TLookupRequest structure.
  163.  
  164.         OTMemzero(&lookupRequest, sizeof(lookupRequest));
  165.         lookupRequest.name.buf = (UInt8 *) requestAddress;
  166.         lookupRequest.name.len = OTStrLength(requestAddress);
  167.         lookupRequest.timeout = 1000;                        // 1 second in milliseconds
  168.         lookupRequest.maxcnt = kResponseBufferSize / kNBPEntityBufferSize;
  169.  
  170.         // Set up the TLookupReply structure.
  171.         
  172.         OTMemzero(&lookupReply, sizeof(lookupReply));
  173.         lookupReply.names.buf = responseBuffer;
  174.         lookupReply.names.maxlen = kResponseBufferSize;
  175.         
  176.         // Call OT synchronously.
  177.         
  178.         err = OTLookupName(nbpMapper, &lookupRequest, &lookupReply);
  179.     }
  180.     
  181.     // Print out the contents of the responseBuffer.
  182.     
  183.     if (err == noErr) {
  184.         printf("\n");
  185.         
  186.         // Start by pointing currentLookupReplyBuffer to point to the
  187.         // beginning of the response buffer.
  188.         
  189.         currentLookupReplyBuffer = (TLookupBuffer *) responseBuffer;
  190.         
  191.         // For each response in the buffer...
  192.         
  193.         for (nameIndex = 0; nameIndex < lookupReply.rspcount; nameIndex++) {
  194.  
  195.             // ... print the name and address and...
  196.         
  197.             printf("%3d ", nameIndex);
  198.             PrintAddress( (DDPAddress *) ¤tLookupReplyBuffer->fAddressBuffer[0]);
  199.             PrintName( (char *) ¤tLookupReplyBuffer->fAddressBuffer[currentLookupReplyBuffer->fAddressLength], currentLookupReplyBuffer->fNameLength);
  200.             printf("\n");
  201.             
  202.             // ... use OTNextLookupBuffer to get from the current buffer to the next.
  203.             
  204.             currentLookupReplyBuffer = OTNextLookupBuffer(currentLookupReplyBuffer);
  205.         }
  206.     }
  207.     
  208.     // Clean up.
  209.     
  210.     if (responseBuffer != nil) {
  211.         OTFreeMem(responseBuffer);
  212.     }
  213.     if (nbpMapper != kOTInvalidMapperRef) {
  214.         junk = OTCloseProvider(nbpMapper);
  215.         OTAssert("LookupAndPrint: Failed closing mapper", junk == noErr);
  216.     }
  217.     
  218.     return (err);
  219. }
  220.  
  221. /////////////////////////////////////////////////////////////////////
  222.  
  223. void main(void)
  224. {
  225.     OSStatus err;
  226.     char requestAddress[] = "=:AFPServer@*";
  227.     
  228.     printf("Hello Cruel World!\n");
  229.     
  230.     err = InitOpenTransport();
  231.     
  232.     if (err == noErr) {
  233.     
  234.         err = LookupAndPrint(requestAddress);
  235.         
  236.         CloseOpenTransport();
  237.     }
  238.     
  239.     if (err == noErr) {
  240.         printf("Success.\n");
  241.     } else {
  242.         printf("Failed with error %d.\n", err);
  243.     }
  244.     printf("Done.  Press command-Q to Quit.\n");
  245. }
  246.